package org.eclipse.wtp.sample.flashcards;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FlashCardServlet extends HttpServlet implements Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		boolean debug = true;
		if (false) {
			System.out.print("FlashCardButton:");
			System.out.println(request.getParameter("FCButton"));
			System.out.print("New Card Button:");
			System.out.println(request.getParameter("NewCardSubmit"));
		}
		int buttonID = 0;
		String buttonName = request.getParameter("FCButton");
		if		(buttonName==null)
				buttonName = request.getParameter("NewCardSubmit");
		if		(buttonName==null)
				System.err.println("WTF both buttons NULL!");
			
		if      (buttonName.equals("Random Card"))
				buttonID=1;
		else if (buttonName.equals("Next Card"))
				buttonID=2;
		else if (buttonName.equals("Prev Card"))
				buttonID=3;
		else if (buttonName.equals("Flip Card"))
				buttonID=7;
		else if (buttonName.equals("Add Card"))
				buttonID=4;
		else if (buttonName.equals("Remove Card"))
				buttonID=5;
		else if (buttonName.equals("Submit"))
				buttonID=6;
		if (false) {
			System.out.println(buttonID);
		}
		
//		Map<Integer,String> deck = (Map<Integer,String>) 
//				request.getSession(true).getAttribute("deck");
		ArrayList<String> deck    = (ArrayList<String>) 
				request.getSession(true).getAttribute("deck");
		Integer currentKey        = (Integer) 
				request.getSession(true).getAttribute("currentKey");
		Boolean side 			 = (Boolean) 
				request.getSession(true).getAttribute("side");
//		Map<String,String> deck = new HashMap<String,String>();
//		Integer currentKey = 0;
//		Integer maxKey = 0;
		String cardFront = "NULL STRING";
		String cardBack = "NULL STRING";
//		Boolean side = true;
		if (deck == null) {
			deck = new ArrayList<String>();
			//deck = new HashMap<Integer,String>();
		}
		if (deck.isEmpty()) {
			currentKey = 0;
		} 
		if (side == null)
			side = true;
		if (debug) {
			System.out.println("currentKey:" + currentKey);
			System.out.println("deck.size():" + deck.size());
		}
	
		switch (buttonID) {
		case 1: //The random card button
				Random generator = new Random();
				if (!deck.isEmpty()){ 
					currentKey = generator.nextInt(deck.size());
				} else {
					currentKey = 0;
				}
			break;
		case 2: //The next card button
			if (currentKey != null) {
				currentKey += 1;
				if (currentKey >= deck.size())
					currentKey = 0;
			}
			break;
		case 3: //The previous card button
			if (currentKey != null) {
				currentKey -= 1;
				if (currentKey < 0)
					currentKey = deck.size()-1;
			}
			break;
		case 4: //The new card button
			getServletContext().getRequestDispatcher("/NewCard.jsp").forward(request, response);
			break;
		case 5: //The remove card button
			if (deck != null && !deck.isEmpty()) {
				deck.remove(currentKey.intValue());
				currentKey -= 1;
		request.getSession().setAttribute("deck", deck);
				if (deck.isEmpty()){
					currentKey = 0;
					break;
				}
				if (currentKey <= 0) {
					currentKey = deck.size()-1;
				}
			}
			break;
		case 6: //The new card submit button
			cardFront = request.getParameter("front");
			cardBack = request.getParameter("back");
			if (debug) {
				System.out.println("front: " + cardFront + "\n back: "
						+ cardBack + deck.size());
			}
			if (deck != null && cardFront != null && cardBack != null) {
				deck.add(cardFront+","+cardBack);
				currentKey = deck.size()-1;
			}
			break;
		case 7: //The flip card submit button
			if (side)
				side = false;
			else
				side = true;
			break;
		default:
			System.out.println("Erroneous Button Name returned");
		}
		if (deck != null && !deck.isEmpty()) {
			cardFront = deck.get(currentKey).split(",")[0];
			cardBack = deck.get(currentKey).split(",")[1];
		}
		request.getSession().setAttribute("side", side);
		request.getSession().setAttribute("deck", deck);
		request.getSession().setAttribute("theFront", cardFront);
		request.getSession().setAttribute("theBack", cardBack);
		request.getSession().setAttribute("currentKey", currentKey);
		if (buttonID != 4)
			getServletContext().getRequestDispatcher("/FlashCards.jsp").forward(request, response);
	}
}
